package com.zfl;

public class MaximalSquare {


    public int maximalSquare(char[][] matrix) {
        int maxSquare=0;
        int row = matrix.length;
        int col = matrix[0].length;

        int[][] dp = new int[row][col];
        for (int i = 0; i < row; i++) {
            dp[i][0] = matrix[i][0] == '1'?1:0;
            maxSquare = Math.max(maxSquare,dp[i][0]);
        }
        for (int i = 0; i < col; i++) {
            dp[0][i] = matrix[0][i]=='1'?1:0;
            maxSquare = Math.max(maxSquare,dp[0][i]);
        }

        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                if (matrix[i][j]=='0') {
                    dp[i][j] = 0;
                    continue;
                }

                dp[i][j] = Math.min(dp[i-1][j],Math.min(dp[i][j-1],dp[i-1][j-1]))+1;
                maxSquare = Math.max(maxSquare,dp[i][j]*dp[i][j]);
            }
        }
        return maxSquare;
    }
}